<?php

namespace app\admin\controller;

use app\admin\model\Admin;
use app\admin\model\User;
use app\admin\model\Order;
use app\admin\model\active\Active;
use app\admin\model\Withdraw;
use app\common\controller\Backend;
use app\common\model\Attachment;
use fast\Date;
use think\Db;
use think\Session;


/**
 * 控制台
 *
 * @icon   fa fa-dashboard
 * @remark 用于展示当前系统中的统计数据、统计报表及重要实时数据
 */
class Dashboard extends Backend
{

    protected $noNeedLogin = ['selecttime'];
    /**
     * 查看
     */
    public function index()
    {
        try {
            \think\Db::execute("SET @@sql_mode='';");
        } catch (\Exception $e) {

        }
        $admininfo=$this->auth->getUserInfo();
        

        //展示公司总数，车辆总数，项目总数

        //展示待审核的车辆数，待审核的公司数，待审核的项目数


        //执法总数，执法违规数


        $storecount=db('store')->count();
        $storecount1=db('store')->where(['status'=>'2'])->count();
        $carcount=db('car')->count();
        $carcount1=db('car')->where(['status'=>'2'])->count();
        $productcount=db('product')->count();
        $productcount1=db('product')->where(['status'=>'2'])->count();
        $violationcount=db('violation')->count();
        $violationcount1=db('violation')->where(['is_illegal'=>'1'])->count();


        //如果是运输公司，展示被拒绝的车辆数，项目数，违法记录数，展示公司审核状态
        $carcount_store=0;
        $productcount_store=0;
        $violationcount_store=0;
        $store_status=0;
        if($admininfo['store_id'])
        {
            $carcount_store=db('car')->where(['status'=>'4','store_id'=>$admininfo['store_id']])->count();
            $productcount_store=db('product')->where(['status'=>['in','4,5'],'store_id'=>$admininfo['store_id']])->count();
            $violationcount_store=db('violation')->where(['is_illegal'=>'1','store_id'=>$admininfo['store_id']])->count();
            $store_status=db('store')->where(['id'=>$admininfo['store_id']])->value('status');
        }

        $this->view->assign([
            'storecount'=>$storecount,
            'storecount1'=>$storecount1,
            'carcount'=>$carcount,
            'carcount1'=>$carcount1,
            'productcount'=>$productcount,
            'productcount1'=>$productcount1,
            'violationcount'=>$violationcount,
            'violationcount1'=>$violationcount1,
            'carcount_store'=>$carcount_store,
            'productcount_store'=>$productcount_store,
            'violationcount_store'=>$violationcount_store,
            'store_status'=>$store_status,
            'store_id'=>$admininfo['store_id']
        ]);
        // $this->view->assign('guoQi',$guoQi);
        // $this->assignconfig('column', array_keys($userlist));
        // $this->assignconfig('userdata', array_values($userlist));

        
        



        return $this->view->fetch();
    }

    function tj()
    {
        return $this->view->fetch();
    }

    //工作台根据时间搜索利润
    public function selecttime()
    {
        $selectTime = $this->request->param('selecttime');
        $where = "isfa = '1'";
        $whereFhz = "user_del = '0'";
        $whereP = "user_del = '0'";
        $whereF = "user_del = '0'";
        $wherePz = "user_del = '0'";
        $whereFh = "user_del = '0'";
        $whereSUC = "";
        if (!empty($selectTime)) {
            $selectTimeArr = explode(' - ',$selectTime);
            $startTime = "";
            $endTime = "";
            if (isset($selectTimeArr[0])) {
                $startTime = strtotime($selectTimeArr[0]);
            }
            if (isset($selectTimeArr[1])) {
                $endTime = strtotime($selectTimeArr[1]);
            }

            if (!empty($startTime)) {
                $where.=" AND fa_time >= '{$startTime}'";
                $whereFhz.=" AND authen_time >= '{$startTime}'";
                $whereP.=" AND addtime >= '{$startTime}'";
                $whereF.=" AND addtime >= '{$startTime}'";
                $wherePz.=" AND addtime >= '{$startTime}'";
                $whereFh.=" AND addtime >= '{$startTime}'";
                $whereSUC.="successtime >= '{$startTime}'";
            }

            if (!empty($endTime)) {
                $where.=" AND fa_time < '{$endTime}'";
                $whereFhz.=" AND authen_time < '{$endTime}'";
                $whereP.=" AND addtime < '{$endTime}'";
                $whereF.=" AND addtime < '{$endTime}'";
                $wherePz.=" AND addtime < '{$endTime}'";
                $whereFh.=" AND addtime < '{$endTime}'";
                $whereSUC.=" AND successtime < '{$endTime}'";
            }
        }
        if ($this->roleType == 'zongbu') {
            $sqll = "SELECT
            cai_order_id,
            store_id,
            sum( goods_price ) AS goods_price,
            sum( cost_price ) AS cost_price,
            sum( salesman_price ) AS salesman_price,
            sum( promotion_price_pt ) AS promotion_price_pt,
            sum( center_price ) AS center_price,
            sum( goods_price - cost_price - salesman_price - promotion_price_pt - center_price ) AS newprice 
        FROM
            jianji_order_goods_store 
            WHERE ".$where;

            //var_dump($sqll);die;
            $platformPrice = db()->query($sqll);
            $fenH = db('order_bonus')->where(['authen'=>2,'to_store_id'=>0,'user_del'=>'0'])->sum('price');
            foreach ($platformPrice as $kkk =>$vvv) {
                $platformPrice[$kkk]['xs_price'] = bcsub($vvv['goods_price'],$vvv['cost_price'],2);
                $platformPrice[$kkk]['salesman_price'] = round($vvv['salesman_price'],2);
                $platformPrice[$kkk]['promotion_price_pt'] = round($vvv['promotion_price_pt'],2);
                $platformPrice[$kkk]['center_price'] = round($vvv['center_price'],2);
                $platformPrice[$kkk]['promotion_price'] = bcadd($vvv['promotion_price_pt'],$vvv['center_price'],2);
                $platformPrice[$kkk]['newprice'] = bcsub($vvv['newprice'],$fenH,2);
                $platformPrice[$kkk]['fh'] = $fenH;
            }

            return json([
                'code'=>1,
                'msg'=>'获取成功',
                'list'=>$platformPrice[0]
            ]);
        } else {
            //中心空间
            $admininfo=$this->auth->getUserInfo();
            $store_info=db('store')->where('id',$admininfo['store_id'])->find();

            //推广总佣金
            $promitionPrice = db('promition_log')
                ->where(['promotion_id'=>$admininfo['store_id'],'type'=>1,'promotion_type'=>$store_info['type']])
                ->where($whereP)
                ->sum('price');
            //销售利润
            $orderLirun = db('order_goods_store')
                ->where(['store_id'=>$admininfo['store_id']])
                ->where('authen','in',[0,3])
                ->where('successtime','>',0)
                ->where("order_id",'>',0)
                ->where($whereSUC)
                ->sum('lirun');

            //本中心/空间的分红
            $fenH = db('order_bonus')
                ->where(['promotion_id'=>$admininfo['store_id'],'promotion_type'=>$store_info['type'],'authen'=>2])
                ->where($whereFh)
                ->sum('price');

            //分销支出
            $fenxiaoZc = db('fenxiao_log')
                ->where('store_id',$admininfo['store_id'])
                ->where($whereF)
                ->sum('price');

            //推广合伙人支出
            $promitionZc = db('promition_log')
                ->where('store_id',$admininfo['store_id'])
                ->where($wherePz)
                ->sum('price');

            //分红支出
            $fenHz = db('order_bonus')
                ->where(['to_store_id'=>$admininfo['store_id'],'authen'=>2])
                ->where($whereFhz)
                ->sum('price');

            return json([
                'code'=>1,
                'msg'=>'获取成功',
                'list'=>[
                    'promitionPrice'=>$promitionPrice,
                    'orderLirun'=>$orderLirun,
                    'fenH'=>$fenH,
                    'fenxiaoZc'=>$fenxiaoZc,
                    'promitionZc'=>$promitionZc,
                    'fenHz'=>$fenHz,
                ]
            ]);
        }

    }

}
